/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/9/30  10:24
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.ut.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/**
 * Created by baichuan on 2016/9/30.
 */
public class BaseSurfaceView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder mHolder;  //surface Holdler
    private Canvas mCanvas;         // 画布
    private Thread thread;          // 绘图子线程
    private boolean isRunning = true;

    public static int SPEED = 50;

    public BaseSurfaceView(Context context) {
        super(context);
        initSelf(null, 0);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSelf(attrs, 0);
    }

    public BaseSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initSelf(attrs, defStyleAttr);
    }

    /**
     * 初始化view
     *
     * @param attrs
     * @param defStyleAttr
     */
    private void initSelf(AttributeSet attrs, int defStyleAttr) {
        mHolder = getHolder();
        mHolder.addCallback(this);
        // 设置画布透明
        // setZOrderOnTop(true); // 设置改view在屏幕的 最顶部
        mHolder.setFormat(PixelFormat.TRANSLUCENT);

        //设置 可以获取焦点
        setFocusable(true);
        setFocusableInTouchMode(true);
        //设置屏幕常亮
        setKeepScreenOn(true);
    }


    /***************************************************/

    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        isRunning = true;
        thread = new Thread(this);
        thread.start();
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        isRunning = false;
    }

    /***************************************************/
    @Override
    public void run() {
        // AntLog.i("run");
        while (isRunning) {

            long star = System.currentTimeMillis();
            try {
                mCanvas = mHolder.lockCanvas();
                if (mCanvas != null) {
                    // AntLog.i("running while ant");
                    draws(mCanvas);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
            }
            long end = System.currentTimeMillis();

            try {
                if (end - star < SPEED) {
                    Thread.sleep(SPEED - (end - star));
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }


    /**
     * 用来会绘图的方法
     *
     * @param mCanvas
     */
    public void draws(Canvas mCanvas) {

    }

}